package sccs.core;

import java.util.ArrayList;
import java.util.List;

import javax.faces.component.UIComponent;
import javax.faces.model.SelectItem;

import org.apache.log4j.Logger;
import org.richfaces.component.html.HtmlTab;
import org.richfaces.component.html.HtmlTabPanel;

import ar.edu.sccs.dao.DaoModule;
import ar.edu.sccs.dao.ProjectDTO;
import ar.edu.sccs.dao.RepositoryDTO;
import ar.edu.sccs.dao.SystemDTO;
import ar.edu.sccs.model.loader.CoreLoader;
import ar.edu.sccs.model.loader.dto.DataClazz;
import ar.edu.sccs.model.loader.dto.DataProject;
import ar.edu.sccs.model.loader.dto.DataSCM;
import ar.edu.sccs.monitoring.exception.SccsGenericException;
import ar.edu.sccs.monitoring.logging.SCCSLogger;
import ar.edu.sccs.util.Constants;
import ar.edu.sccs.util.ObjectOutStream;

/**
 * Clase backend del JSP codeSearchForm
 * 
 */
public class ProjectBean {

	static Logger logger = SCCSLogger.getLogger(ProjectBean.class);

	private CoreLoader coreLoader;
	private DaoModule daoModule;

	private ProjectDTO project;
	private RepositoryDTO repository;

	private String clientName;
	private String clientBiz;
	private String clientUsrQty;
	private String clientTxQty;
	private String tecnicalStandard;
	private String tecnicalJVM;
	private String tecnicalFwk;

	private boolean renderTabGrid = false;
	private HtmlTabPanel tabPanel;

	private List<SelectItem> repositoryProtocolList;
	private List<SelectItem> projectStateList;

	public ProjectBean() {
		coreLoader = new CoreLoader();
		daoModule = new DaoModule();

		initComboLists();
		initDimensions();
	}

	private List<SystemDTO> sys_pProtocolList;
	private List<SystemDTO> sys_pState;
	private void initComboLists() {

		repositoryProtocolList = new ArrayList<SelectItem>();
		projectStateList = new ArrayList<SelectItem>();

		try {

			if (sys_pProtocolList == null || sys_pState == null) {
				sys_pProtocolList = daoModule.findSystem(Constants.Parameter.OPTION_PROTOCOLTYPE);
				sys_pState = daoModule.findSystem(Constants.Parameter.OPTION_STATETYPE);
			}

			for (SystemDTO systemDTO : sys_pProtocolList)
				repositoryProtocolList.add(new SelectItem(systemDTO.getSystem_key(), systemDTO.getSystem_value()));
			for (SystemDTO systemDTO : sys_pState)
				projectStateList.add(new SelectItem(systemDTO.getSystem_key(), systemDTO.getSystem_value()));
		} catch (SccsGenericException e) {
			//
		}
	}

	private void initDimensions() {
		project = new ProjectDTO();
		repository = new RepositoryDTO();
	}

	public void download() {
		logger.info("ProjectBean.download(): " + project.toString() + repository.toString());

		DataClazz clazz = new DataClazz();
		clazz.setClazzRepository(new DataSCM(repository));
		clazz.setClazzProject(new DataProject(project));

		List<DataClazz> dataClazzList = new ArrayList<DataClazz>();
		dataClazzList.add(clazz);

		ObjectOutStream oos = new ObjectOutStream();
		try {
			oos.writeDataClazzFile(System.getProperty("jboss.server.home.dir") + Constants.Loader.FILE_NAME, dataClazzList);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void upload() {
		logger.info("ProjectBean.upload()");
	}

	public void clean() {
		logger.info("ProjectBean.clean()");

		initComboLists();
		initDimensions();

		List<UIComponent> tabs = tabPanel.getChildren();
		((HtmlTab) tabs.get(1)).setActive(false);
		tabPanel.setSelectedTab("tabDownload");
		tabPanel.getChildren().clear();
	}

	public String exit() {
		logger.info("CodeSearchBean.exit()");

		clean();
		return "logOut";
	}

	// ================================================= //

	public ProjectDTO getProject() {
		return project;
	}

	public CoreLoader getCoreLoader() {
		return coreLoader;
	}

	public void setCoreLoader(CoreLoader coreLoader) {
		this.coreLoader = coreLoader;
	}

	public DaoModule getDaoModule() {
		return daoModule;
	}

	public void setDaoModule(DaoModule daoModule) {
		this.daoModule = daoModule;
	}

	public void setProject(ProjectDTO project) {
		this.project = project;
	}

	public RepositoryDTO getRepository() {
		return repository;
	}

	public void setRepository(RepositoryDTO repository) {
		this.repository = repository;
	}

	public String getClientName() {
		return clientName;
	}

	public void setClientName(String clientName) {
		this.clientName = clientName;
	}

	public String getClientBiz() {
		return clientBiz;
	}

	public void setClientBiz(String clientBiz) {
		this.clientBiz = clientBiz;
	}

	public String getClientUsrQty() {
		return clientUsrQty;
	}

	public void setClientUsrQty(String clientUsrQty) {
		this.clientUsrQty = clientUsrQty;
	}

	public String getClientTxQty() {
		return clientTxQty;
	}

	public void setClientTxQty(String clientTxQty) {
		this.clientTxQty = clientTxQty;
	}

	public String getTecnicalStandard() {
		return tecnicalStandard;
	}

	public void setTecnicalStandard(String tecnicalStandard) {
		this.tecnicalStandard = tecnicalStandard;
	}

	public String getTecnicalJVM() {
		return tecnicalJVM;
	}

	public void setTecnicalJVM(String tecnicalJVM) {
		this.tecnicalJVM = tecnicalJVM;
	}

	public String getTecnicalFwk() {
		return tecnicalFwk;
	}

	public void setTecnicalFwk(String tecnicalFwk) {
		this.tecnicalFwk = tecnicalFwk;
	}

	public HtmlTabPanel getTabPanel() {
		return tabPanel;
	}

	public void setTabPanel(HtmlTabPanel tabPanel) {
		this.tabPanel = tabPanel;
	}

	public List<SelectItem> getRepositoryProtocolList() {
		return repositoryProtocolList;
	}

	public void setRepositoryProtocolList(List<SelectItem> repositoryProtocolList) {
		this.repositoryProtocolList = repositoryProtocolList;
	}

	public List<SelectItem> getProjectStateList() {
		return projectStateList;
	}

	public void setProjectStateList(List<SelectItem> projectStateList) {
		this.projectStateList = projectStateList;
	}

	public List<SystemDTO> getSys_pProtocolList() {
		return sys_pProtocolList;
	}

	public void setSys_pProtocolList(List<SystemDTO> sys_pProtocolList) {
		this.sys_pProtocolList = sys_pProtocolList;
	}

	public List<SystemDTO> getSys_pState() {
		return sys_pState;
	}

	public void setSys_pState(List<SystemDTO> sys_pState) {
		this.sys_pState = sys_pState;
	}
}